﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DLL;
using System.Data;
using System.Data.SqlClient;

namespace BLL
{
   public class NhaCungCapBLL
    {
       DataServer DB = new DataServer();
       public DataTable Select_All()
       {
           string sql = "select *from NhaCC";
           DataTable bang = DB.LayDulieu(sql);
           return bang;
       }
       public bool Them(NhaCungCapDAL ncc)
       {
           string sql = string.Format("sp_NhaCungCap_Insert");
           string MaNCC = DLL.CommonFunc.GetPrimaryKey("NhaCungCap");
           SqlParameter paramMa = new SqlParameter("@MaNCC", MaNCC);
           SqlParameter paramTen = new SqlParameter("@TenNCC", ncc.TenNCC);
           SqlParameter paramDC = new SqlParameter("@DiaChi", ncc.DiaChi);
           SqlParameter paramDT = new SqlParameter("@DienThoai", ncc.DienThoai);
           SqlParameter paramEmail = new SqlParameter("@Mail", ncc.EMail);
           try
           {
               DB.CapNhatProc(sql, paramMa, paramTen, paramDC, paramDT, paramEmail);
           }
           catch
           {
               return false;
           }
           DLL.CommonFunc.UpdatePrimaryKey("NhaCungCap");
           return true;
       }
       public bool Sua(NhaCungCapDAL ncc)
       {
           string sql = string.Format("sp_NhaCungCap_Update");
          
           SqlParameter paramMa = new SqlParameter("@MaNCC", ncc.MaNCC);
           SqlParameter paramTen = new SqlParameter("@TenNCC", ncc.TenNCC);
           SqlParameter paramDC = new SqlParameter("@DiaChi", ncc.DiaChi);
           SqlParameter paramDT = new SqlParameter("@DienThoai", ncc.DienThoai);
           SqlParameter paramEmail = new SqlParameter("@Mail", ncc.EMail);
           try
           {
               DB.CapNhatProc(sql, paramMa, paramTen, paramDC, paramDT, paramEmail);
           }
           catch
           {
               return false;
           }
          
           return true;
       }
       public int Xoa(string Ma)
       {
           int kq = 0;
           string sql = string.Format("sp_NhaCungCap_Delete");
           SqlParameter paramMTB = new SqlParameter("@MaNCC", Ma);
           SqlParameter paramKQ = new SqlParameter("@Ketqua", SqlDbType.Int);
           paramKQ.Direction = ParameterDirection.Output;
           DB.CapNhatProc(sql, paramMTB, paramKQ);
           kq = int.Parse(paramKQ.Value.ToString());
           return kq;
       }
       public bool Kiemtra(string Ma)
       {
           string sql = string.Format("sp_NhaCungCap_Kiemtra");
           SqlParameter paramMNTB = new SqlParameter("@MaNCC", Ma);
           DataTable bang = DB.LayDuLieuProc(sql, paramMNTB);
           if (bang.Rows.Count > 0)
           {
               return true;
           }
           return false;
       }
       public DataTable Search(NhaCungCapDAL ncc)
       {
           string sql = string.Format("sp_NhaCungCap_Search");
           SqlParameter paramMTB = new SqlParameter("@MaNCC", ncc.MaNCC);
           SqlParameter paramTTB = new SqlParameter("@TenNCC", ncc.TenNCC);
           DataTable bang = DB.LayDuLieuProc(sql, paramMTB, paramTTB);
           return bang;
       }
       public bool Kiemtraten(string Ten)
       {
           string sql = "select TenNCC from NhaCC where TenNCC=@TenNCC";
           SqlParameter paramMNTB = new SqlParameter("@TenNCC", Ten);
           DataTable bang = DB.LayDulieu(sql, paramMNTB);
           if (bang.Rows.Count > 0)
           {
               return true;
           }
           return false;
       }
    }
}
